

$(function() {
	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/Attend/getDatas",
		paramFunc: paramFunc,
		operation: {
			// "icon-list": ["Admin/Attend/edit/#id",'课堂教学评价表',false,900],
			"icon-list": [printAttend1,'课堂教学评价表',false,1200],
			"edit": [editAttend,'编辑'],//"Admin/Class_Assess/attendAdd/#id"
			"icon-print": [printAttend,'打印'],
			"icon-reply": [replyAttend,'退回'],
			"icon-trash": [deleteAttend,'删除'],
		},
		hasCheckbox: true,
		afterFunc:afterFunc
	});

	laydate.render({
	    elem: '#start_time', //指定元素
	    trigger: 'click'
	});
	laydate.render({
	    elem: '#end_time', //指定元素
	    trigger: 'click'
	});

	// exportEvent();

	$(".resetButton").click(function(event) {
		$("input[name='listen_name']").val('');
		$('.searchButton').click();
	});

	$('#dept_name').searchableSelect();

	$("#dept_name").next().find('.searchable-select-item').click(function () {
		$("#dept_name").next().find('.searchable-select-item').removeClass("selected");
		$(this).addClass("selected");
		$('.searchButton').click();
	});

	$('#course_units').searchableSelect();

	$("#course_units").next().find('.searchable-select-item').click(function () {
		$("#course_units").next().find('.searchable-select-item').removeClass("selected");
		$(this).addClass("selected");
		$('.searchButton').click();
	});


});

var paramFunc = function() {
	var data = getFormJson();

	return data;
};

var afterFunc = function(){

    $("#table tbody tr").each(function() {
        var data = $("#table").getOpts().datas[$(this).index()];
        if(data.admin_type==1){
        	$(this).children("td:nth-child(3)").find(".icon-lock").hide();
        }
        if(data.is_show == 1){
        	$(this).children("td:nth-child(3)").find(".icon-reply").show();
        }else if(data.is_show == 2){
        	$(this).children("td:nth-child(3)").find(".icon-reply").hide();
        }
		if(data.color){
			$(this).css('color',data.color)
		}
    });
}

var printAttend = function(idx, trData){

	//window.location.href="Admin/Attend/edit/"+trData.id+"?";
	if(trData.cc_type == '2' && trData.created>1660548484){
		window.open("Admin/Attend/edit/"+trData.id+"?type=2");
	}else{
		window.open("Admin/Attend/edit/"+trData.id+"?type=1");
	}
	
}
var printAttend1 = function(idx, trData){

	//window.location.href="Admin/Attend/edit/"+trData.id+"?";
	if(trData.cc_type == '2' && trData.created>1660548484){
		showDialogModal("Admin/Attend/edit/"+trData.id+"?type=2?");
	}else{
		showDialogModal("Admin/Attend/edit/"+trData.id+"?type=1?");
	}
	
}

var replyAttend = function(idx, trData){
	var msg = "是否确认退回听课【" + trData.listen_name + "】的听课记录?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		/* if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Attend/reply/"+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		} */
		if (isConfirm) {
            showDialogModalConfirm(
                '',
                '请填写退回理由',
                '<p>理由</p><textarea type="text" id="reason" rows="3" style="width:100%"></textarea>',
                '确认',
                '取消',
                function(){
                    var reason=$('#reason').val();
                    // d = dialog({
                    //     title: '',
                    //     content: "退回中,请稍后...",
                    // });
                    // d.showModal();
                    $.ajax({
                        type: "post",
                        url: "Admin/Attend/reply/"+trData.id,
                        async: true,
                        data:{reason:reason},
                        success: function(data) {                    
                            var ret = $.parseJSON(data);

                            console.log(ret);
                            if (ret.status) {
                                window.parent.topMessagePrompt('success', ret.msg);
                                window.parent.refreshTable();
                                // window.location.reload();//页面重新刷新
                                // hideDialogModal();
								setTimeout(function() {
									// 根据实际实现选择关闭方式
									if(window.parent.topMessageClose) {
										window.parent.topMessageClose();
									} else {
										// 尝试其他关闭方式
									}
								}, 3000);
                            } else {
                                window.parent.topMessagePrompt('error', ret.msg);
                                window.parent.refreshTable();
                                // window.location.reload();//页面重新刷新
                                // hideDialogModal();
								setTimeout(function() {
									// 根据实际实现选择关闭方式
									if(window.parent.topMessageClose) {
										window.parent.topMessageClose();
									} else {
										// 尝试其他关闭方式
									}
								}, 3000);
                            }
                        }
                    });
                },
                function(){
                    hideDialogModal();
                });
            
        }


	});
}


var deleteAttend = function(idx, trData) {
	var msg = "是否确认删除该听课?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Attend/delete/"+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
};

//导出数据
exportEvent = function() {
	$("input").keypress(function(event) {
		if(event.keyCode == "13") {
			$("#table").search();
		}
	})
	$("#export").click(function() {
		var ret = $("#table").getTableThInfo();
		ret.params = usefulArray(paramFunc());
		ret.title = "会员列表";
		locationPost("User/commonExport", {
			'json': JSON.stringify(ret)
		});
	});
};


function Export(){		
	var data = getFormJson();
	var msg = "请确认是否导出?";
    swal({
        title: "请确认",
        text: msg,
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: true,
        confirmButtonText: "确定",
        cancelButtonText: '取消',
        confirmButtonColor: "#ec6c62"
    }, function(isConfirm) {
        if(isConfirm) {
            var load_alert=loadAlert('数据导出中，请稍后...');
            $.ajax({
                type: "post",
                url: "Admin/Attend/getDatas/1/true",
                data:data,
                async: true,
                dataType:'json',
                success: function(data) {
                    closeAlert(load_alert);
                    if(data.status) {
                        location.href=getRootPath()+'/'+data.v;
                    } else {
                        window.parent.topMessagePrompt('error',ret.msg);
                    }
                }
            });
        }
    });
}


//批量提交听课数据
function moreSubmit(){
	var id=$('.xrTable').getCheckItems('id');
	console.log('id',id);
	if(id.length == 0){
		window.parent.topMessagePrompt('error','请打勾选择需要提交的课堂教学评价!');
	}else {
		console.log(id);
		var msg = "";
		msg = "提交后无法修改,是否确认提交?";
		swal({
			title: "请确认",
			text: msg,
			type: "warning",
			showCancelButton: true,
			closeOnConfirm: true,
			confirmButtonText: "确定",
			cancelButtonText: '取消',
			confirmButtonColor: "#ec6c62"
		}, function(isConfirm) {
			if(isConfirm) {
				$.ajax({
					type: "post",
					url: "Admin/Class_Assess/moreSubmit",
					dataType:'json',
					data:{
						id:id,
					},
					success: function(data) {
						console.log('data',data)
						if(data.status) {
							window.parent.topMessagePrompt('success',data.msg);
							window.parent.refreshTable();
						} else {
							window.parent.topMessagePrompt1('error',data.msg);
						}
					}
				});
			}
		});

	}

}

var editAttend = function(idx, trData){
	 showAdd(trData);
	//window.location.href="Admin/Attend/edit/"+trData.id+"?";
	// window.open("Admin/Attend/edit/"+trData.id+"?");
}

//添加课堂教学评价
function showAdd(trData){
	console.log(trData);
	// console.log(getRootPath());
	// console.log( $('input[name="url"]').val());
	var Url =  $('input[name="url"]').val();
	var id = trData.id;
	var cc_type =  trData.cc_type;
	var created =  trData.created;
	var status =  trData.status;
	// console.log(id);
	// console.log(cc_type);
	//2022-08-15 15:28:04之前的沿用老的课程类型，2022-08-15 15:28:04之后的沿用新的课程类型
	if(typeof(id) != "undefined" && typeof(created) != "undefined" && created<1660548484){
		console.log("2022-08-15以前的老数据");
		showDialogModal(Url + "/Admin/Class_Assess/attendAdd/1/"+id);
	}else{
		console.log("2022-08-15之后的新数据");

		console.log(id);


		if(typeof(status) != "undefined" && status != 1){ //只有状态未提交时可切换课程类型
			if(cc_type == 1){
				showDialogModal(Url + "/Admin/Class_Assess/attendAdd/1/"+id);
			}else if(cc_type == 2 && created>1660548484){
				showDialogModal(Url + "/Admin/Class_Assess/attendAdd/2/"+id);
			}else if(cc_type == 3){
				showDialogModal(Url + "/Admin/Class_Assess/attendAdd/3/"+id);
			}else{
				showDialogModal(Url + "/Admin/Class_Assess/attendAdd/1/"+id);
			}
		}else{
			$.ajax({
				type: "post",
				url: "Admin/Attend/getClassType",
				dataType:'json',
				data:{
					id:id,
				},
				success: function(data) {
					console.log('data',data)
					if(data) {
						var html = "<select id='team'  placeholder='请选择' style='border-radius: 0;-webkit-box-shadow: none!important;box-shadow: none!important;color: #858585;background-color: #fff;border: 1px solid #d5d5d5!important;'>";
						$.each(data.class_type, function (k, v) {
							if(cc_type == k+1){
								html += "<option value='"+(k+1)+"' selected>"+v['option']+"</option>";	
							}else{
								html += "<option value='"+(k+1)+"'>"+v['option']+"</option>";
							}		
						});
						html += "</select>";
						showDialogModalConfirm(
							'',
							'请选择课程类型',
							html,
							'确定',
							'取消',
							function () {
								var team_id = $("#team").val();

								if(typeof(id) != "undefined"){
									showDialogModal(Url + "/Admin/Class_Assess/attendAdd/" + team_id+"/"+id);
								}else{
									showDialogModal(Url + "/Admin/Class_Assess/attendAdd/" + team_id+"/0");
								}
							
								// showDialogModal(Url + "/Admin/Class_Assess/attendAdd/" + team_id+"/0");
							},
							function () {
								hideDialogModal();
							});

					} else {
						window.parent.topMessagePrompt('error','课程类型无数据');
					}
				}
			});

			// var html = "<select id='team'  placeholder='请选择' style='border-radius: 0;-webkit-box-shadow: none!important;box-shadow: none!important;color: #858585;background-color: #fff;border: 1px solid #d5d5d5!important;'>";
			// if(class_type == '理论课'){
			// 	html += "<option value='1' selected>理论课</option>";
			// }else{
			// 	html += "<option value='1'>理论课</option>";
			// }
			// // html += "<option value='1'>理论课</option>";
			// if(class_type == '实训课'){
			// 	html += "<option value='2' selected >实训课</option>";
			// }else{
			// 	html += "<option value='2'>实训课</option>";
			// }
			// html += "</select>";
		

		}
		
		
		// showDialogModal('<?=base_url()?>Admin/Class_Assess/attendAdd/0');
	}


}

function printAll(){
	var id=$('.xrTable').getCheckItems('id');
	console.log('id',id);
	if(id.length == 0){
		window.parent.topMessagePrompt('error','请打勾选择需要打印的课堂教学评价!');
	}else if (id.length > 500){
		window.parent.topMessagePrompt('error','单次打印不能超过最大限制500条!');
	}else {
		console.log(id);
		showDialogModal("Admin/Attend/printAll/"+id,1100,800);
	}
}

function rebackAll(){
	var id=$('.xrTable').getCheckItems('id');
	// console.log('id',id);
	if(id.length == 0){
		window.parent.topMessagePrompt('error','请打勾选择需要打印的课堂教学评价!');
	}else if (id.length > 500){
		window.parent.topMessagePrompt('error','单次打印不能超过最大限制500条!');
	}else {
		console.log(id);
		// showDialogModal("Admin/Attend/printAll/"+id,1100,800);

		var msg = "";
		msg = "是否确认提交?";
		swal({
			title: "请确认",
			text: msg,
			type: "warning",
			showCancelButton: true,
			closeOnConfirm: true,
			confirmButtonText: "确定",
			cancelButtonText: '取消',
			confirmButtonColor: "#ec6c62"
		}, function(isConfirm) {
			if(isConfirm) {
				$.ajax({
					type: "post",
					url: "Admin/Class_Assess/rebackAll",
					dataType:'json',
					data:{
						id:id,
					},
					success: function(data) {
						console.log('data',data)
						if(data.status) {
							window.parent.topMessagePrompt('success',data.msg);
							window.parent.refreshTable();
						} else {
							window.parent.topMessagePrompt1('error',data.msg);
						}
					}
				});
			}
		});
	}
}


//批量打印全部
// function printAll(){
// 	var data = $("#searchForm").serialize();
// 	var class_name = $("input[name='class_name']").val();
// 	var listen_class = $("input[name='listen_class']").val();
// 	var teacher_name = $("input[name='teacher_name']").val();
// 	var listen_name = $("input[name='listen_name']").val();
// 	var content = $("input[name='content']").val();
// 	var start_time = $("input[name='start_time']").val();
// 	var end_time = $("input[name='end_time']").val();
// 	var title = $("#title").val();
// 	var class_type = $("#class_type").val();
// 	var type = $("#type").val();
// 	var dept_name = $("#dept_name").val();
// 	var course_units = $("#course_units").val();
// 	var listen_type = $("#listen_type").val();
// 	var is_submit = $("#is_submit").val();
// 	var score = $("#score").val();
// 	var is_complete = $("#is_complete").val();
// 	if(class_name == "" && listen_class == "" && teacher_name == "" && listen_name=="" && content =="" && start_time=="" && end_time=="" 
// 	&& title == "-1" && class_type == "-1" && type=="-1" && dept_name == "-1" && course_units == "-1" && listen_type == "-1" && is_submit == "-1"
// 	&& score == "-1"&& is_complete == "-1"){
// 		window.parent.topMessagePrompt('error','请选择搜索条件');
// 	}else{
// 		showDialogModal("Admin/Attend/printAll?"+data,1100,800);
// 	}
// }


